package com.hopes.bizz.risk.applet.service.impl;

import com.hopes.bizz.risk.applet.service.AppletStatisticsService;
import com.hopes.bizz.risk.applet.statistics.vo.TaskCount;
import com.hopes.bizz.risk.common.constant.FlowConstant;
import com.hopes.bizz.risk.common.constant.HidDicConstant;
import com.hopes.bizz.risk.common.constant.RiskDicConstant;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskRecordEntity;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskRecordService;
import com.hopes.bizz.risk.hid.entity.RiskHidCheckTaskEntity;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerDelayEntity;
import com.hopes.bizz.risk.hid.service.RiskHidCheckTaskService;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerDelayService;
import com.hopes.boss.act.dto.FlowTaskQuery;
import com.hopes.boss.act.feign.RemoteActivitiService;
import com.hopes.boss.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author: ljh
 * @create: 2023/7/25
 */
@Service
@RequiredArgsConstructor
public class AppletStatisticsServiceImpl implements AppletStatisticsService {


    private final RiskMeasureTaskRecordService riskMeasureTaskRecordService;

    private final RiskHidCheckTaskService riskHidCheckTaskService;

    private final RemoteActivitiService remoteActivitiService;

    private final RiskHidHiddenDangerDelayService riskHidHiddenDangerDelayServiceService;

    @Override
    public TaskCount statisticsTaskCount() {
        String userId = SecurityUtils.getUserIdStr();
        // 根据工作流查询当前用户下的任务
        TaskCount taskCount = new TaskCount();
        FlowTaskQuery flowTaskQuery = new FlowTaskQuery();
        flowTaskQuery.setDefinitionKey(FlowConstant.HIDDEN_DANGER);
        flowTaskQuery.setUserId(userId);

        // 隐患待确定
        flowTaskQuery.setCode(HidDicConstant.HID_DANGER_STATUS_WAIT_CONFIRM.toString());
        taskCount.setHiddenDangerWaitConfirm(Optional.ofNullable(remoteActivitiService.countTodo(flowTaskQuery).getData()).orElse(0L));
        // 隐患待整改
        flowTaskQuery.setCode(HidDicConstant.HID_DANGER_WAIT_RECTIFICATION.toString());
        taskCount.setHiddenDangerReform(Optional.ofNullable(remoteActivitiService.countTodo(flowTaskQuery).getData()).orElse(0L));
        // 隐患待验收
        flowTaskQuery.setCode(HidDicConstant.HID_DANGER_STATUS_WAIT_ACCEPTANCE.toString());
        taskCount.setHiddenDangerAccept(Optional.ofNullable(remoteActivitiService.countTodo(flowTaskQuery).getData()).orElse(0L));
        // 延期申请
        taskCount.setHiddenDangerPostpone(riskHidHiddenDangerDelayServiceService.lambdaQuery()
                .eq(RiskHidHiddenDangerDelayEntity::getHandleUserId, SecurityUtils.getUserId())
                .eq(RiskHidHiddenDangerDelayEntity::getHandleStatus, HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_UNDISPOSED)
                .count());
        // 双体系任务数
        taskCount.setTwoSystemTaskCount(riskMeasureTaskRecordService.lambdaQuery()
                .like(RiskMeasureTaskRecordEntity::getResponsibleUserIds, userId)
                .eq(RiskMeasureTaskRecordEntity::getTaskStatus, RiskDicConstant.HIDDEN_CHECK_TASK_STATUS_NOT_FINISH)
                .count());
        // 隐患排查任务
        taskCount.setHiddenDangerCheckCount(riskHidCheckTaskService.lambdaQuery()
                .eq(RiskHidCheckTaskEntity::getRecorder, userId)
                .eq(RiskHidCheckTaskEntity::getTaskStatus, RiskDicConstant.RISK_HID_CHECKLIST_TASK_STATUS_TODO)
                .count());

        return taskCount;
    }

}
